import React from 'react';
import { Button, Table, Modal, message, Spin } from 'antd';
import Api from 'Utils/api';
import AddModal from '../Modal/addRoleModal';
import EqualBorder from './../../../CommonComponents/EqualBorder';
import './index.scss';

const confirm = Modal.confirm;
const PAGE_SIZE = 10;
class Role extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      dataCount: 0,
      data: [],
      listPage: 1,
      isLoading: true,
    };
    this.condition = { offset: 0, limit: PAGE_SIZE };
    this.initList = ::this.initList;
    this.onAdd = ::this.onAdd;
  }
  componentDidMount() {
    this.initList();
  }
  initList() {
    const api = new Api({
      method: 'GET',
      path: '/roles',
      query: { ...this.condition },
      success: (json) => {
        this.setState({ isLoading: false, listPage: ((this.condition.offset / PAGE_SIZE) + 1), data: json.data_list, dataCount: json.total_count });
      },
      error: (msg) => {
        this.setState({
          isLoading: false
        });
        message.error(msg);
      }
    });
    api.run();
  }
  onListPageChange(page) {
    this.setState({ listPage: page, isLoading: true }, this.initList.bind(this));
    this.condition.offset = (page - 1) * PAGE_SIZE;
  }
  generatorColumns() {
    return [
      {
        title: '角色名称',
        dataIndex: 'name',
        render: text => (
          <span >{text}</span >
        )
      },
      {
        title: '描述',
        dataIndex: 'description',
        render: text => (
          <span >{text}</span >
        )
      },
      {
        title: '操作',
        dataIndex: 'operate',
        render: (text, record) => (
          <span className="operate">
            <span className="edit" onClick={this.onAdd.bind(this, record)}>修改</span >
            <span className="delete" onClick={this.handleDelete.bind(this, record)}>删除</span >
          </span>
        )
      }
    ];
  }
  onAdd(record) {
    this.form.showModal(record);
  }
  handleDelete(record) {
    const self = this;
    confirm({
      title: '删除',
      content: '确定删除该角色吗？',
      onOk() {
        self.setState({
          isLoading: true,
        });
        const api = new Api({
          method: 'DELETE',
          path: `/roles/${record.id}`,
          success: () => {
            self.initList();
          },
          error: (msg) => {
            message.error(msg);
          }
        });
        api.run();
      },
      onCancel() {
        console.log('Cancel');
      },
    });
  }
  render() {
    const columns = this.generatorColumns();
    return (
      <div>
        <EqualBorder
          btn={
            <Button className="search-btn" onClick={this.onAdd}>
              <img src="images/page/icon_add.png" alt="" />
              新增
            </Button>
          }
          content={
            <Spin size="large" spinning={this.state.isLoading} tip="加载中...">
              <Table
                rowKey={record => record.id}
                columns={columns}
                bordered
                dataSource={this.state.data}
                pagination={{
                  pageSize: PAGE_SIZE,
                  total: this.state.dataCount,
                  current: this.state.listPage,
                  onChange: this.onListPageChange.bind(this)
                }}
              />
            </Spin>
          }
        />
        <AddModal
          ref={(ref) => { this.form = ref; }}
          onSure={this.initList}
        />
      </div>
    );
  }
}

export default Role;
